package client

import (
	"fmt"
	"log"
	"os"
	"osdesign/buffer"
	"strings"
	"time"

	"github.com/gookit/color"
	"github.com/jroimartin/gocui"
)

type Operator struct {
	free int
	now  int
	b    *buffer.Buff
	sum  int
}

var opt *Operator
var flag bool
var read []string
var put int
var get int

func check(s string) bool {
	for i := 0; i < len(s); i++ {
		if s[i] == '-' {
			continue
		}
		if s[i] < '0' || s[i] > '9' {
			return false
		}
	}
	return true
}

func to_int(s string) int {
	var x int = 0
	if s[0] == '-' {
		for i := 1; i < len(s); i++ {
			x = x*10 + int(s[i]-'0')
		}
		x = -x
	} else {
		for i := 0; i < len(s); i++ {
			x = x*10 + int(s[i]-'0')
		}
	}
	return x
}

func ini(g *gocui.Gui) *Operator {
	v, _ := g.View("out")
	v.Clear()
	var buff_c, p1_c, p2_c, p3_c, p1_n, p2_n, p3_n, putt, gett, sum int
	if buff, err := g.View("buff"); err == nil {
		cap := buff.Buffer() + " "
		capslice := strings.Split(cap, " ")
		for i := 0; i < 4; i++ {
			tmp := to_int(capslice[i])
			if i > len(capslice)-1 {
				tmp = 1
			}
			if i == 0 {
				buff_c = tmp
			}
			if i == 1 {
				p1_c = tmp
			}
			if i == 2 {
				p2_c = tmp
			}
			if i == 3 {
				p3_c = tmp
			}
		}
		// fmt.Fprint(v, color.Yellow.Text(cap))
		// fmt.Fprintf(v, "\n%s + %s + %s + %d", capslice[0], capslice[1], capslice[2], len(capslice[3]))
		// fmt.Fprintf(v, "\n%d + %d + %d + %d", buff_c, p1_c, p2_c, p3_c)
	}
	if p1, err := g.View("p1"); err == nil {
		p1_n = to_int(strings.Split(p1.Buffer(), " ")[0])
	} else {
		p1_n = 1
	}
	if p2, err := g.View("p2"); err == nil {
		p2_n = to_int(strings.Split(p2.Buffer(), " ")[0])
	} else {
		p2_n = 1
	}
	if p3, err := g.View("p3"); err == nil {
		p3_n = to_int(strings.Split(p3.Buffer(), " ")[0])
	} else {
		p3_n = 1
	}
	if put, err := g.View("put"); err == nil {
		putt = to_int(strings.Split(put.Buffer(), " ")[0])
	} else {
		putt = 3
	}
	if get, err := g.View("get"); err == nil {
		gett = to_int(strings.Split(get.Buffer(), " ")[0])
		// for i := 0; i < len(get.Buffer()); i++ {
		// 	fmt.Fprintf(v, "%d !! \n", get.Buffer()[i])
		// }
		// fmt.Fprintf(v, "%d @\n", gett)
	} else {
		gett = 1
	}
	fmt.Fprintf(v, "%d %d %d %d %d\n", p1_n, p2_n, p3_n, putt, gett)
	bf := buffer.New(p1_c, p2_c, p3_c, putt, gett, buff_c)
	put = putt
	get = gett
	sum = p1_n + p2_n + p3_n
	return &Operator{
		free: sum,
		b:    bf,
		now:  0,
		sum:  sum,
	}
}

func ts(g *gocui.Gui) {
	ticker := time.NewTicker(time.Second)
	flag = true
	for i := range ticker.C {
		if !flag {
			ticker.Stop()
			break
		}
		opt.now++
		g.Update(func(g *gocui.Gui) error {
			viw, _ := g.View("out")
			vposi, _ := g.View("posi")
			vnega, _ := g.View("nega")
			vzero, _ := g.View("zero")
			year := i.Year()     // 年
			month := i.Month()   // 月
			day := i.Day()       // 日
			minute := i.Minute() // 分钟
			second := i.Second() // 秒
			go opt.b.Do()
			// viw.Clear()
			// pt := "now time : "
			// fmt.Fprintf(viw, "\n%v\n", color.Yellow.Text(pt))
			fmt.Fprintf(viw, color.FgGreen.Text("%v-%v-%v %v:%v \nthe com start at %v s \n"), year, month, day, minute, second, opt.now)
			// fmt.Fprintf(viw, color.FgGreen.Text("%v : %v \n"), opt.b.Getlfget(), opt.b.Getlfput())
			fmt.Fprint(viw, "all number in buffer : ")
			for x := opt.b.Getlis().Front(); x != nil; x = x.Next() {
				fmt.Fprintf(viw, color.FgGreen.Text("%v "), x.Value)
			}
			fmt.Fprintf(viw, "\n")
			fmt.Fprintf(viw, "the blocked threads number : %v\n", opt.b.Getblock())
			// fmt.Fprintf(viw, "the free    threads number : %v", opt.sum-opt.b.Getblock())
			fmt.Fprintf(viw, "\n---------------------\n")
			// fmt.Fprint(viw, color.FgGreen.Text(string("--posi: \n")))
			lis1 := opt.b.Getp1()
			vposi.Clear()
			vnega.Clear()
			vzero.Clear()
			for x := 0; x < len(lis1); x++ {
				fmt.Fprintf(vposi, color.FgGreen.Text("%v "), lis1[x])
			}
			// fmt.Fprint(viw, color.FgGreen.Text(string("\n--nega: \n")))
			lis2 := opt.b.Getp2()
			for x := 0; x < len(lis2); x++ {
				fmt.Fprintf(vnega, color.FgGreen.Text("%v "), lis2[x])
			}
			// fmt.Fprint(viw, color.FgGreen.Text(string("\n----------\n")))
			lis3 := opt.b.Getp3()
			for x := 0; x < len(lis3); x++ {
				fmt.Fprintf(vzero, "%v ", lis3[x])
			}
			return nil
		})
	}
}

func ts2(g *gocui.Gui) {
	ticker := time.NewTicker(time.Second)
	flag = true
	sz := len(read)
	// tt, _ := g.View("out")
	// for i := 0; i < sz; i++ {
	// 	fmt.Fprintf(tt, " !! \n %d \n ", to_int(read[i]))
	// }
	for i := range ticker.C {
		if !flag {
			ticker.Stop()
			break
		}
		var v int = 1
		if opt.now < sz {
			// if !check(read[opt.now]) {
			// 	continue
			// }
			v = to_int(read[opt.now])
			// fmt.Fprintf(tt, "!! \n %s  -> %d\n", read[opt.now], to_int(read[opt.now]))
		}
		opt.now++
		g.Update(func(g *gocui.Gui) error {
			viw, _ := g.View("out")
			vposi, _ := g.View("posi")
			vnega, _ := g.View("nega")
			vzero, _ := g.View("zero")
			year := i.Year()     // 年
			month := i.Month()   // 月
			day := i.Day()       // 日
			minute := i.Minute() // 分钟
			second := i.Second() // 秒
			// if opt.now < sz {
			// 	opt.b.Undo(v)
			// } else {
			opt.b.Undo(v)
			// }
			// viw.Clear()
			// pt := "now time : "
			// fmt.Fprintf(viw, "\n!!!%v!!!\n", v)
			fmt.Fprintf(viw, color.FgGreen.Text("%v-%v-%v %v:%v \nthe com start at %v s \n"), year, month, day, minute, second, opt.now)
			// fmt.Fprintf(viw, color.FgGreen.Text("%v : %v \n"), opt.b.Getlfget(), opt.b.Getlfput())
			fmt.Fprint(viw, "all number in buffer : ")
			vm := getlis(g, opt.now)
			// for x := opt.b.Getlis().Front(); x != nil; x = x.Next() {
			// 	fmt.Fprintf(viw, color.FgGreen.Text("%v "), x.Value)
			// }
			for x := 0; x < len(vm); x++ {
				fmt.Fprintf(viw, color.FgGreen.Text("%v "), vm[x])
			}
			fmt.Fprintf(viw, "\n")
			fmt.Fprintf(viw, "the blocked threads number : %v\n", opt.b.Getblock())
			// fmt.Fprintf(viw, "the free    threads number : %v", opt.sum-opt.b.Getblock())
			fmt.Fprintf(viw, "\n---------------------\n")
			// fmt.Fprint(viw, color.FgGreen.Text(string("--posi: \n")))
			// lis1 := opt.b.Getp1()
			vposi.Clear()
			vnega.Clear()
			vzero.Clear()
			lis1 := getlis1(g, opt.now)
			for x := 0; x < len(lis1); x++ {
				fmt.Fprintf(vposi, color.FgGreen.Text("%v "), lis1[x])
			}
			// fmt.Fprint(viw, color.FgGreen.Text(string("\n--nega: \n")))
			// lis2 := opt.b.Getp2()
			lis2 := getlis2(g, opt.now)
			for x := 0; x < len(lis2); x++ {
				fmt.Fprintf(vnega, color.FgGreen.Text("%v "), lis2[x])
			}
			// fmt.Fprint(viw, color.FgGreen.Text(string("\n----------\n")))
			// lis3 := opt.b.Getp3()
			lis3 := getlis3(g, opt.now)
			for x := 0; x < len(lis3); x++ {
				fmt.Fprintf(vzero, "%v ", lis3[x])
			}
			return nil
		})
	}
}

/*
100 -100 0 10 3 -2 0 -9 8
*/
func start(g *gocui.Gui, cv *gocui.View) error {
	opt = ini(g)
	go ts(g)
	return nil
}

func start2(g *gocui.Gui, cv *gocui.View) error {
	opt = ini(g)
	readl, _ := g.View("main")
	read = strings.Split(readl.Buffer(), " ")
	// tt, _ := g.View("out")
	// for i := 0; i < len(read); i++ {
	// 	fmt.Fprintf(tt, " !! \n %s \n ", read[i])
	// }
	go ts2(g)
	return nil
}

func stop(g *gocui.Gui, cv *gocui.View) error {
	flag = false
	return nil
}

func RunMain() {
	//step2 创建 GUI 图层对象并进行参与回调函数的配置
	g, err := gocui.NewGui(gocui.OutputNormal)
	if err != nil {
		log.Panicln(err)
	}

	g.Cursor = true
	g.Mouse = true
	g.ASCII = false
	g.SetManagerFunc(layout)
	setPage(g)
	setCtrlC(g)
	setCtrlB(g)
	setCtrlD(g)
	setCtrlM(g)

	setMouseLeft(g)
	setMouseWheel(g)
	if err := g.MainLoop(); err != nil {
		log.Panicln(err)
	}
}

func layout(g *gocui.Gui) error {
	maxX, maxY := g.Size()
	split := (maxX) / 6
	sli := 2
	h := 2
	if err := viewbuff(g, 1, 1, split-sli, 1+h); err != nil {
		return err
	}
	if err := viewp1(g, split+1, 1, split*2-sli, 1+h); err != nil {
		return err
	}
	if err := viewp2(g, split*2+1, 1, split*3-sli, 1+h); err != nil {
		return err
	}
	if err := viewp3(g, split*3+1, 1, split*4-sli, 1+h); err != nil {
		return err
	}
	if err := viewput(g, split*4+1, 1, split*5-sli, 1+h); err != nil {
		return err
	}
	if err := viewget(g, split*5+1, 1, split*6-sli, 1+h); err != nil {
		return err
	}
	if err := viewInput(g, 1, 4, maxX-1, 6); err != nil {
		return err
	}
	wid := (maxY - 6) / 3
	lf := maxX / 3
	if err := viewOutput(g, 1, 7, lf, maxY-1); err != nil {
		return err
	}
	if err := viewposi(g, lf+1, 7, maxX-1, 7+wid); err != nil {
		return err
	}
	if err := viewnega(g, lf+1, 7+wid+1, maxX-1, 7+2*wid); err != nil {
		return err
	}
	if err := viewZero(g, lf+1, 7+2*wid+1, maxX-1, maxY-1); err != nil {
		return err
	}
	return nil
}

func quit(g *gocui.Gui, v *gocui.View) error {
	openFile, _ := os.OpenFile("1.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 777)
	viw, _ := g.View("out")
	s := viw.BufferLines()
	for i := 0; i < len(s); i++ {
		openFile.WriteString(s[i] + "\n")
	}
	g.Close()
	return gocui.ErrQuit
}

func viewUpScroll(g *gocui.Gui, cv *gocui.View) error {
	v, err := g.View("out")
	v.Autoscroll = false
	ox, oy := v.Origin()
	if err == nil {
		v.SetOrigin(ox, oy-1)
	}
	return nil
}

func viewDownScroll(g *gocui.Gui, cv *gocui.View) error {
	v, err := g.View("out")
	_, y := v.Size()
	ox, oy := v.Origin()
	lnum := len(v.BufferLines())
	if err == nil {
		if oy > lnum-y-1 {
			v.Autoscroll = true
		} else {
			v.SetOrigin(ox, oy+1)
		}
	}
	return nil
}

func setMouseWheel(g *gocui.Gui) {
	if err := g.SetKeybinding("out", gocui.MouseWheelUp, gocui.ModNone, viewUpScroll); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("out", gocui.MouseWheelDown, gocui.ModNone, viewDownScroll); err != nil {
		log.Panicln(err)
	}
}

func getlis1(g *gocui.Gui, v int) []int {
	a := make([]int, 0)
	for i := 0; i < len(read); i++ {
		if (i+1)*put > v || (i+1)*get > v {
			break
		}
		if to_int(read[i]) > 0 {
			a = append(a, to_int(read[i]))
		}
	}
	return a
}

func getlis2(g *gocui.Gui, v int) []int {
	a := make([]int, 0)
	for i := 0; i < len(read); i++ {
		if (i+1)*put > v || (i+1)*get > v {
			break
		}
		if to_int(read[i]) < 0 {
			a = append(a, to_int(read[i]))
		}
	}
	return a
}

func getlis3(g *gocui.Gui, v int) []int {
	a := make([]int, 0)
	for i := 0; i < len(read); i++ {
		if (i+1)*put > v || (i+1)*get > v {
			break
		}
		if to_int(read[i]) == 0 {
			a = append(a, to_int(read[i]))
		}
	}
	return a
}

func getlis(g *gocui.Gui, v int) []int {
	a := make([]int, 0)
	for i := 0; i < len(read); i++ {
		if (i+1)*put > v {
			break
		}
		if (i+1)*put <= v && (i+1)*get > v {
			a = append(a, to_int(read[i]))
		}
	}
	return a
}

func viewOutput(g *gocui.Gui, x0, y0, x1, y1 int) error {
	v, err := g.SetView("out", x0, y0, x1, y1)
	if err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Wrap = true
		v.Overwrite = false
		v.Autoscroll = true
		v.SelBgColor = gocui.ColorRed
		v.Title = "Messages"
	}
	return nil
}

func viewInput(g *gocui.Gui, x0, y0, x1, y1 int) error {
	if v, err := g.SetView("main", x0, y0, x1, y1); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		//当 err == gocui.ErrUnknownView 时运行
		v.Editable = true
		v.Wrap = true
		v.Overwrite = false
		v.Title = "input"
		if _, err := g.SetCurrentView("main"); err != nil {
			return err
		}
	}
	return nil
}

func viewbuff(g *gocui.Gui, x0, y0, x1, y1 int) error {
	if v, err := g.SetView("buff", x0, y0, x1, y1); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Editable = true
		v.Overwrite = false
		v.Wrap = true
		title := "buffer capactity"
		v.Title = title
	}
	return nil
}

func viewp1(g *gocui.Gui, x0, y0, x1, y1 int) error {
	if v, err := g.SetView("p1", x0, y0, x1, y1); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Title = "Number of P1 threads"
		v.Editable = true
		v.Overwrite = false
		v.Wrap = true
	}
	return nil
}

func viewp2(g *gocui.Gui, x0, y0, x1, y1 int) error {
	if v, err := g.SetView("p2", x0, y0, x1, y1); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Title = "Number of P2 threads"
		v.Editable = true
		v.Overwrite = false
		v.Wrap = true
	}
	return nil
}

func viewp3(g *gocui.Gui, x0, y0, x1, y1 int) error {
	if v, err := g.SetView("p3", x0, y0, x1, y1); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Title = "Number of P3 threads"
		v.Editable = true
		v.Overwrite = false
		v.Wrap = true
	}
	return nil
}

func viewput(g *gocui.Gui, x0, y0, x1, y1 int) error {
	if v, err := g.SetView("put", x0, y0, x1, y1); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Title = "PUT time limit"
		v.Editable = true
		v.Overwrite = false
		v.Wrap = true
	}
	return nil
}

func viewget(g *gocui.Gui, x0, y0, x1, y1 int) error {
	if v, err := g.SetView("get", x0, y0, x1, y1); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Title = "GET time limit"
		v.Editable = true
		v.Overwrite = false
		v.Wrap = true
	}
	return nil
}

func viewposi(g *gocui.Gui, x0, y0, x1, y1 int) error {
	if v, err := g.SetView("posi", x0, y0, x1, y1); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Title = "Positive"
		v.Editable = true
		v.Overwrite = false
		v.Wrap = true
	}
	return nil
}

func viewnega(g *gocui.Gui, x0, y0, x1, y1 int) error {
	if v, err := g.SetView("nega", x0, y0, x1, y1); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Title = "Negative"
		v.Editable = true
		v.Overwrite = false
		v.Wrap = true
	}
	return nil
}

func viewZero(g *gocui.Gui, x0, y0, x1, y1 int) error {
	if v, err := g.SetView("zero", x0, y0, x1, y1); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Title = "Zero"
		v.Editable = true
		v.Overwrite = false
		v.Wrap = true
	}
	return nil
}

func viewchange(g *gocui.Gui, cv *gocui.View) error {
	v, err := g.SetCurrentView(cv.Name())
	if err != nil {
		return err
	}
	v.Overwrite = true
	v.Editable = true
	return nil
}

func setPage(g *gocui.Gui) {
	if err := g.SetKeybinding("main", gocui.KeyPgup, gocui.ModNone, viewUpScroll); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("main", gocui.KeyPgdn, gocui.ModNone, viewDownScroll); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("out", gocui.KeyPgup, gocui.ModNone, viewUpScroll); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("out", gocui.KeyPgdn, gocui.ModNone, viewDownScroll); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("posi", gocui.KeyPgup, gocui.ModNone, viewUpScroll); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("posi", gocui.KeyPgdn, gocui.ModNone, viewDownScroll); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("nega", gocui.KeyPgup, gocui.ModNone, viewUpScroll); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("nega", gocui.KeyPgdn, gocui.ModNone, viewDownScroll); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("zero", gocui.KeyPgup, gocui.ModNone, viewUpScroll); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("zero", gocui.KeyPgdn, gocui.ModNone, viewDownScroll); err != nil {
		log.Panicln(err)
	}
}

func setCtrlC(g *gocui.Gui) {
	if err := g.SetKeybinding("main", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("out", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("buff", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("p1", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("p2", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("p3", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("put", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("get", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
		log.Panicln(err)
	}
}

func setCtrlD(g *gocui.Gui) {
	if err := g.SetKeybinding("main", gocui.KeyCtrlD, gocui.ModNone, stop); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("out", gocui.KeyCtrlD, gocui.ModNone, stop); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("buff", gocui.KeyCtrlD, gocui.ModNone, stop); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("p1", gocui.KeyCtrlD, gocui.ModNone, stop); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("p2", gocui.KeyCtrlD, gocui.ModNone, stop); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("p3", gocui.KeyCtrlD, gocui.ModNone, stop); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("put", gocui.KeyCtrlD, gocui.ModNone, stop); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("get", gocui.KeyCtrlD, gocui.ModNone, stop); err != nil {
		log.Panicln(err)
	}
}

func setMouseLeft(g *gocui.Gui) {
	if err := g.SetKeybinding("main", gocui.MouseLeft, gocui.ModNone, viewchange); err != nil {
		log.Panic(err)
	}
	if err := g.SetKeybinding("out", gocui.MouseLeft, gocui.ModNone, viewchange); err != nil {
		log.Panic(err)
	}
	if err := g.SetKeybinding("buff", gocui.MouseLeft, gocui.ModNone, viewchange); err != nil {
		log.Panic(err)
	}
	if err := g.SetKeybinding("p1", gocui.MouseLeft, gocui.ModNone, viewchange); err != nil {
		log.Panic(err)
	}
	if err := g.SetKeybinding("p2", gocui.MouseLeft, gocui.ModNone, viewchange); err != nil {
		log.Panic(err)
	}
	if err := g.SetKeybinding("p3", gocui.MouseLeft, gocui.ModNone, viewchange); err != nil {
		log.Panic(err)
	}
	if err := g.SetKeybinding("put", gocui.MouseLeft, gocui.ModNone, viewchange); err != nil {
		log.Panic(err)
	}
	if err := g.SetKeybinding("get", gocui.MouseLeft, gocui.ModNone, viewchange); err != nil {
		log.Panic(err)
	}
}

func setCtrlB(g *gocui.Gui) {
	if err := g.SetKeybinding("main", gocui.KeyCtrlB, gocui.ModNone, start); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("out", gocui.KeyCtrlB, gocui.ModNone, start); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("buff", gocui.KeyCtrlB, gocui.ModNone, start); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("p1", gocui.KeyCtrlB, gocui.ModNone, start); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("p2", gocui.KeyCtrlB, gocui.ModNone, start); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("p3", gocui.KeyCtrlB, gocui.ModNone, start); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("put", gocui.KeyCtrlB, gocui.ModNone, start); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("get", gocui.KeyCtrlB, gocui.ModNone, start); err != nil {
		log.Panicln(err)
	}
}

func setCtrlM(g *gocui.Gui) {
	if err := g.SetKeybinding("main", gocui.KeyCtrlM, gocui.ModNone, start2); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("out", gocui.KeyCtrlM, gocui.ModNone, start2); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("buff", gocui.KeyCtrlM, gocui.ModNone, start2); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("p1", gocui.KeyCtrlM, gocui.ModNone, start2); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("p2", gocui.KeyCtrlM, gocui.ModNone, start2); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("p3", gocui.KeyCtrlM, gocui.ModNone, start2); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("put", gocui.KeyCtrlM, gocui.ModNone, start2); err != nil {
		log.Panicln(err)
	}
	if err := g.SetKeybinding("get", gocui.KeyCtrlM, gocui.ModNone, start2); err != nil {
		log.Panicln(err)
	}
}
